package com.alumni_circle.controller.security;

import cn.hutool.core.util.ObjectUtil;
import com.alumni_circle.core.config.RedisCache;
import com.alumni_circle.core.config.SecurityConfig;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.entity.rbac.response.*;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.RsaUtils;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.entity.me.VO.UserUpdateVO;
import com.alumni_circle.service.me.impl.UserServiceImpl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 龙小虬
 * @since 2020-08-04 00:36
 */
@Controller
@ResponseBody
public class UserSecurityController extends BaseController {

    @Resource
    UserServiceImpl userService;

    @Resource
    RedisCache redisCache;


    @ResponseBody
    @RequestMapping(value = "/user/info")
    @SysLog(type="查",accessInfo="获取用户数据",url="/user/info")
    public ResponseModel userInfo1(){
        SecurityUtils.getPostId();
        ActionModel[] actionsModels = new ActionModel[1];
        actionsModels[0] = new ActionModel();
        String string = redisCache.getCacheSet(SecurityUtils.getRole()+"(permission)").toString();
        String[] str = string.substring(1,string.length()-1).split(", ");
        PermissionsModel[] permissionsModels = new PermissionsModel[str.length];
        System.out.println(str);
        for (int i = 0; i < str.length; i++) {
            permissionsModels[i] = new PermissionsModel(str[i],actionsModels);
        }
        UserEntity user = ModelUtils.transform(SecurityUtils.getUser(),UserEntity.class);
        System.out.println(SecurityUtils.getUser());
        return (new ResponseModel<>(new RoleModel<>(new ResultModel(permissionsModels),user)));
    }

    @ResponseBody
    @RequestMapping(value = "/user/password",method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @SysLog(type = "改",accessInfo = "修改密码",url = "/user/password")
    public ResponseModel editPassword(@RequestBody BaseId baseId){
        String password = baseId.getId();
        User user = new User();
        user.setId(SecurityUtils.getId());
        user.setPassword(password.split("%%%")[1]);
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("password",password.split("%%%")[0]);
        userService.update(user,wrapper);
        return new ResponseModel("success");
    }

    @ResponseBody
    @RequestMapping(value = "/user/check")
    @SysLog(type = "查",accessInfo = "校验密码",url = "/user/check")
    public ResponseModel checkPassword(@RequestBody BaseId password){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",SecurityUtils.getId());

        if(password.getId().equals(new RsaUtils().matches(userService.getOne(wrapper).getPassword(),password.getId()))){
            return new ResponseModel("fail");
        }else {
            return new ResponseModel("success");
        }
    }
}
